//
//  FamilyViewController.m
//  play
//
//  Created by zhouLiHuan on 2016/12/12.
//  Copyright © 2016年 zhouLiHuan. All rights reserved.
//

#import "FamilyViewController.h"
#import "FamilyModel.h"
#import "LHTableViewCell.h"
@interface FamilyViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)NSMutableArray *dataArr;

@end

@implementation FamilyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.dataArr = [[NSMutableArray alloc] init];
    NSString *path = [[NSBundle mainBundle]pathForResource:@"familNum" ofType:@"plist"];
    NSMutableArray *arr = [[NSMutableArray alloc] initWithContentsOfFile:path];
    for (int i = 0; i < arr.count; i++) {
        FamilyModel *model = [FamilyModel familWithDict:arr[i]];
        [self.dataArr addObject:model];
    }
    
    [self setUpTableView];

}

- (void)setUpTableView{

    _tableView = [[UITableView alloc] init];
    _tableView.frame = CGRectMake(0,0, viewWidth(), viewHeight());
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [self.view addSubview:_tableView];


}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return self.dataArr.count;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
 
    static NSString *cellId = @"FaimlyCell";
    
    LHTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (cell == nil) {
        
        cell = [[LHTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
        cell.backgroundColor = [UIColor orangeColor];
        cell.familModel = self.dataArr[indexPath.row];
    }
    
    return cell;

}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 80;

}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    FamilyModel *model = self.dataArr[indexPath.row];
    
     NSString *titile = [NSString stringWithFormat:@"确定给%@打电话吗?",model.name];
     UIAlertController *alert = [UIAlertController alertControllerWithTitle:titile message:model.telPhone preferredStyle:UIAlertControllerStyleAlert];
    
    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil]];
    
    [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        UIApplication *app = [UIApplication sharedApplication];
        // 打电话
        NSString *phoneStr = [NSString stringWithFormat:@"tel://%@",model.telPhone];
        [app openURL:[NSURL URLWithString:phoneStr]];
    }]];

    [self presentViewController:alert animated:YES completion:nil];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
